/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

#include "StdAfx.h"
#include ".\mportinfolist.h"

#include <algorithm>

//---------------------------------------------------------------------------

MPortInfoList::MPortInfoList(void)
{
}

MPortInfoList::~MPortInfoList(void)
{
	CleanUp();
}

//---------------------------------------------------------------------------

void MPortInfoList::CleanUp(void)
{
	MPortInfo *mpi;

	for(iter=theVector.begin(); iter!=theVector.end(); iter++){
		mpi = *iter;
		if(mpi!=NULL){
			*iter = NULL;
			delete mpi;
		}
	}
	theVector.clear();
}

//---------------------------------------------------------------------------

MPortInfo* MPortInfoList::CreatePortInfo(void)
{
	MPortInfo* mpi;

	try{
		mpi = new MPortInfo;
	}
	catch(...){
		mpi = NULL;
	}
	
	return mpi;
}

//---------------------------------------------------------------------------

MPortInfo* MPortInfoList::FindPortInfo(MPortInfo* mpiIn)
{
	MPortInfo* mpi;
	MPortInfo* res;

	if(mpiIn==NULL) return NULL;

	res = NULL;
	for(iter=theVector.begin(); iter!=theVector.end() && res==NULL; iter++){
		mpi = *iter;
		if(mpi!=NULL){
			if(mpi->method==mpiIn->method)
				if(mpi->port==mpiIn->port)
					res = mpi;
		}
	}

	return res;
}

//---------------------------------------------------------------------------

bool MPortInfoList::Add(MPortInfo* mpiIn)
{
	MPortInfo* mpi;
	int cnt;

	if(mpiIn==NULL) return true;	// nothing to add

	mpi = CreatePortInfo();
	if(mpi==NULL) return false;

	mpi->LoadFromPointer(mpiIn);

	cnt = (int)theVector.size();
	theVector.insert(theVector.begin(), mpi);
	cnt = (int)theVector.size() - cnt;
	if(cnt!=1){
		delete mpi;
		return false;
	}

	return true;
}

bool MPortInfoList::AddNoDuplicates(MPortInfo* mpiIn)
{
	MPortInfo* mpi;

	mpi = FindPortInfo(mpiIn);
	if(mpi!=NULL) return true;

	return Add(mpiIn);
}

void MPortInfoList::Clear(void)
{
	CleanUp();
}

//---------------------------------------------------------------------------

int MPortInfoList::GetSize(void)
{
	return (int)theVector.size();
}

MPortInfo* MPortInfoList::GetByIdx(int idxIn)
{
	if(idxIn<0)          return NULL;
	if(idxIn>=GetSize()) return NULL;

	return theVector[idxIn];
}

//---------------------------------------------------------------------------

bool MPortInfoGreater(MPortInfo* a1, MPortInfo* a2)
{
	if(a2==NULL) return true;
	if(a1==NULL) return false;
	
	if(a1->port!=a2->port)
		return a1->port < a2->port;

	return (int)a1->method < (int)a2->method;
}

void MPortInfoList::SortTheList(void)
{
	std::sort(theVector.begin(), theVector.end(), MPortInfoGreater);
}

void MPortInfoList::RandomizeList(void)
{
	std::random_shuffle(theVector.begin(), theVector.end());
}

//---------------------------------------------------------------------------
